﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Ashlen.Portal.Controllers;
using MvcContrib.TestHelper;
using System.Web.Mvc;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
using Rhino.Mocks;
using System.Linq;
using Ashlen.Portal.Core;
using Ashlen.Portal.Core.DataInterfaces;
using Ashlen.Portal.Data.SqlServer;

namespace Ashlen.Portal.Tests.Ashlen.Portal.Controllers
{
    [TestFixture]
    public class AccountControllerTests
    {
        private TestControllerBuilder _testController;
        [SetUp]
        public void Setup() {
            _testController = new TestControllerBuilder();
        }

        [Test]
        public void CanRegister()
        {
            var controller = new AccountController(CreateMockSecurityDao(), CreateMockLookupDao(), CreateMockUtilityDao(), null);
            _testController.InitializeController(controller);

            /*ViewResult result =
                controller.Register(null, null, null, null, null, null, null, null, null, 
                null, null, null, null, null, false, false) .AssertViewRendered().ForView("Register");*/
        }

        [Test]
        public void CanUpdate()
        {
            var controller = new AccountController(CreateMockSecurityDao(), CreateMockLookupDao(), CreateMockUtilityDao(), 
                null);
            _testController.InitializeController(controller);
            ViewResult result = controller.Update(null, null, null, null, null, null, null, null, null, null, null, null,
                                                  null, null, null)
                .AssertViewRendered().ForView("Change");
        }

        private static ISecurityDao CreateMockSecurityDao() {
            var mocks = new MockRepository();
            var mockedSecurityDao = mocks.StrictMock<ISecurityDao>();
            Expect.Call(mockedSecurityDao.GetPersonalInfo(default(Guid))).
                IgnoreArguments().Return(GetPersonalInfo());
            Expect.Call(mockedSecurityDao.GetEmailPref(default(Guid))).IgnoreArguments().Return(GetEmailPref());
            mocks.Replay(mockedSecurityDao);

            return mockedSecurityDao;
        }

        private static ILookupDao CreateMockLookupDao() {
            var mocks = new MockRepository();
            var mockedLookupDao = mocks.StrictMock<ILookupDao>();
            Expect.Call(mockedLookupDao.GetCountries()).
                IgnoreArguments().Return(GetCountries());
            Expect.Call(mockedLookupDao.GetStates(null)).IgnoreArguments().Return(GetStates());
            mocks.Replay(mockedLookupDao);

            return mockedLookupDao;
        }

        private static IUtilityDao CreateMockUtilityDao() {
            var mocks = new MockRepository();
            var mockedUtilityDao = mocks.StrictMock<IUtilityDao>();
            Expect.Call(mockedUtilityDao.GetConfig(null, null)).
                IgnoreArguments().Return(GetConfig());
            Expect.Call(mockedUtilityDao.GetConfigBySection(null, null)).IgnoreArguments().Return(GetConfigBySection());
            Expect.Call(mockedUtilityDao.GetFromEmailTemplate(null)).IgnoreArguments().Return(GetFromEmailTemplate());
            Expect.Call(mockedUtilityDao.GetUnprocessedEmails(10)).IgnoreArguments().Return(GetUnProcessedEmails());
            mocks.Replay(mockedUtilityDao);

            return mockedUtilityDao;
        }

        private static List<system_Email> GetUnProcessedEmails()
        {
            var emails = new List<system_Email> {new system_Email()};
            return emails;
        }
        private static string GetFromEmailTemplate()
        {
            return "Welcome {0}.";
        }

        private static List<system_Configuration> GetConfigBySection()
        {
            var configSections = new List<system_Configuration>();
            configSections.Add(new system_Configuration());
            return configSections;    
        }

        private static string GetConfig() {
            return "test"; }

        private static aspnet_UserPersonalInfo GetPersonalInfo() {
            return new aspnet_UserPersonalInfo() {Country="Australia", DateCreated = DateTime.Now, DateOfBirth = DateTime.Now,
            DateUpdated = DateTime.Now, Firstname = "Shailen", Lastname="Sukul", Sex = "M", State="VIC" };
        }

        private static aspnet_UserEmailPreference GetEmailPref() {
            return new aspnet_UserEmailPreference();
        }

        private static List<aspnet_Country> GetCountries()
        {
            var countries = new List<aspnet_Country>();
            countries.Add(new aspnet_Country());
            return countries;
        }

        private static List<aspnet_State> GetStates()
        {
            var states = new List<aspnet_State>();
            states.Add(new aspnet_State());
            return states;
        }
    }
}
